xquery version "0.9-ml"

let $root := xdmp:get-request-field("root")
let $id := xdmp:get-request-field("id")
let $node := xdmp:get-request-field("node")

(:
let $root := "doc('wines.xml')/wines/wine"
let $id := "121"
let $node := "vintage"
:)


	(: given a datastore, an id, and  node, get the value of that node :)
	(: the data store structure is assumed to have a root collection node containing :)
	(: containing a collection of children nodes that have an id to be referenced :)
	(: $root should be a reference to the document and the root node -> doc($doc)/root_node :)
	(: $id is simply a string id. this id is expected to be in the root node provided as id:)
	(: $node is an xml element reference from where the root stops -> /child_node/child_node2/etc...
	:)

	(: create a string that will be evaluated later :)
	(: the context is passed in as $base :)
	(: this allows a concrete rference and a string value :)
	(: to be combined to access data :)
	



	let $s := fn:concat("define variable $base external
					  $base/", $node)

	for $rootNode in xdmp:eval($root)
	let $value := xdmp:eval($s,(xs:QName("base"),$rootNode) )
	where $rootNode/@id=$id

	return
	fn:count($value)


